﻿using Microsoft.AspNetCore.SignalR;
using System.Text.RegularExpressions;

namespace TrafficWebApi.Models
{
    /// <summary>
    /// 连接端子
    /// </summary>
    public class MessageHub : Hub
    {
        #region PageCls
        private Respoitories.Repository<Traffic.Entity.SysAccount>? UserRespository { get; set; }
        #endregion

        /// <summary>
        /// 用户连接
        /// </summary>
        /// <returns></returns>
        public override async Task OnConnectedAsync()
        {
            try
            {
                var ctx = Context.GetHttpContext();
                string? uid = ctx?.Request.Query["uid"];
                if (!string.IsNullOrEmpty(uid))
                {
                    var UserModel = UserRespository?.Query().Select(x => x.Id == int.Parse(uid))?.FirstOrDefault();
                    await Groups.AddToGroupAsync(Context.ConnectionId, UserModel != null ? uid : string.Empty);
                }
                else await Groups.AddToGroupAsync(Context.ConnectionId, string.Empty);
            }
            catch
            {
                await Groups.AddToGroupAsync(Context.ConnectionId, string.Empty);
            }
        }

        public override Task OnDisconnectedAsync(Exception? exception)
        {
            return base.OnDisconnectedAsync(exception);
        }
    }
}